package com.movitech.mobile.cz.base.purchase.listener;

import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.common.util.StringUtils;
import com.movitech.mobile.cz.base.common.utils.SpringContext;
import com.movitech.mobile.cz.base.modules.activiti.entity.AProcessNodeLineConfig;
import com.movitech.mobile.cz.base.modules.activiti.service.AProcessNodeLineConfigService;
import com.movitech.mobile.cz.base.purchase.entity.WFlowFormSteps;
import com.movitech.mobile.cz.base.purchase.service.WFlowFormStepsService;
import com.movitech.mobile.cz.base.purchase.threadlocal.ThreadLocalWFlowFormSteps;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.impl.el.FixedValue;

public class FlowStatusExecutionListener implements ExecutionListener {

	private static final long serialVersionUID = 1L;

	private FixedValue lineConfigId;
	
	@Override
	public void notify(DelegateExecution execution) throws Exception {
		if (StringUtils.equalsIgnoreCase(EVENTNAME_TAKE,execution.getEventName())){
			System.out.println("=========================FlowStatusExecutionListener=============="+execution.getVariable("flowFormId")+"=========="+lineConfigId.getValue(execution));

			WFlowFormStepsService wFlowFormStepsService = (WFlowFormStepsService)SpringContext.getBean("wFlowFormStepsService");
			AProcessNodeLineConfigService aProcessNodeLineConfigService = (AProcessNodeLineConfigService)SpringContext.getBean("aProcessNodeLineConfigService");
  //		ActivitiTaskService activitiTaskService = (ActivitiTaskService)SpringContext.getBean("activitiTaskService");

			AProcessNodeLineConfig aProcessNodeLineConfig = aProcessNodeLineConfigService.selectById(StringUtils.object2StringNotNull(lineConfigId.getValue(execution)));
			if(aProcessNodeLineConfig!=null&&aProcessNodeLineConfig.getLineStatus()!=0) {
				WFlowFormSteps wFlowFormSteps = new WFlowFormSteps();
				wFlowFormSteps.setBaseFeilds(null, null, CommonConstants.NO_INTEGER, true);
				wFlowFormSteps.setFlowFormId(StringUtils.object2StringNotNull(execution.getVariable("flowFormId")));
				wFlowFormSteps.setProcessNodeLineConfigId(StringUtils.object2StringNotNull(lineConfigId.getValue(execution)));
   //			wFlowFormSteps.setTaskId(activitiTaskService.getTaskIdsByExecutionId(execution.getId()));
				wFlowFormSteps.setExecutionId(execution.getId());
				wFlowFormSteps.setLineStatus(aProcessNodeLineConfig.getLineStatus());
				wFlowFormSteps.setDoneFlag(CommonConstants.NO_INTEGER);
				wFlowFormStepsService.insert(wFlowFormSteps);
				////
				//System.out.println(Thread.currentThread().getId()+"-"+Thread.currentThread().getName());
				ThreadLocalWFlowFormSteps threadLocalWFlowFormSteps = ThreadLocalWFlowFormSteps.getInstance();
				threadLocalWFlowFormSteps.setItem(wFlowFormSteps);
			}
		}

	}

	public FixedValue getLineConfigId() {
		return lineConfigId;
	}

	public void setLineConfigId(FixedValue lineConfigId) {
		this.lineConfigId = lineConfigId;
	}

	
}
